package com.io.score;

import java.io.*;
import java.util.*;

/**
 * ScoreManager--bdqnPractice
 *
 * @author: Jovi
 * @createTime: 2017-06-20 15:56
 **/
public class ScoreManager {

    Student stu = null;
    Scanner sc = new Scanner(System.in);
    Map<Integer, String> subjectMap;

    public void init() {
        subjectMap = new HashMap<>();
        Subject subject1 = new Subject(1, "java");
        Subject subject2 = new Subject(2, "sql");
        Subject subject3 = new Subject(3, "jsp");

        subjectMap.put(new Integer(subject1.getSubId()), subject1.getSubName());
        subjectMap.put(new Integer(subject2.getSubId()), subject2.getSubName());
        subjectMap.put(new Integer(subject3.getSubId()), subject3.getSubName());

    }

    public void inputInfo() {
        System.out.println("***Enter Student Infomation***");
        System.out.println("Student Id:");
        int stuId = sc.nextInt();
        System.out.println("Student Name:");
        String stuName = sc.next();
        System.out.println("Student CertNo:");
        String idCard = sc.next();

        List<Score> scoreList = new ArrayList<>();
        System.out.println("Java score：");
        double javaScore = sc.nextDouble();
        Score score1 = new Score(1, 1, javaScore);

        System.out.println("SQL score:");
        double sqlScore = sc.nextDouble();
        Score score2 = new Score(2, 2, sqlScore);

        System.out.println("Jsp score:");
        double jspScore = sc.nextDouble();
        Score score3 = new Score(3, 3, jspScore);

        scoreList.add(score1);
        scoreList.add(score2);
        scoreList.add(score3);

        stu = new Student(stuId, stuName, idCard);
        stu.setScoreList(scoreList);
        System.out.println("The info is saved");

    }

    public void save(String file) {
        ObjectOutputStream oos = null;
        try {
            oos = new ObjectOutputStream(new FileOutputStream(file));
            oos.writeObject(stu);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (oos != null) {
                try {
                    oos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    public void load(String file) {
        ObjectInputStream ois = null;
        try {
            ois = new ObjectInputStream(new FileInputStream(file));
            try {
                Student stu = (Student) ois.readObject();
                List<Score> list = stu.getScoreList();
                System.out.println("The student Infomation is:");
                System.out.println("id\tname\tidCard");
                System.out.println(stu.getStuId() + "\t" + stu.getStuName() + "\t" + stu.getiDcard());
                System.out.println("The student score is :");
                double total = 0;
                for (Score score : list) {
                    String subName = subjectMap.get(score.getScoreId());
                    double stuScore = score.getScore();
                    total += stuScore;
                    System.out.println(subName + "\t" + stuScore);
                }
                System.out.println("The total" + total);
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
